﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public partial class Project
    {
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE Update(Project project, UserInfo userInfo)
        {
            DataClassesDataContext db = null;
            try
            {
                var ID = project.ID;
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                Project pj = db.Projects.Where(s => s.ID == ID).SingleOrDefault();
                if (pj == null)
                {
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.UPDATE_ROW_ERROR;
                }
                pj.ProgramID = project.ProgramID;
                pj.ProjectCode = project.ProjectCode;
                pj.ProjectName = project.ProjectName;
                pj.DefaultMargin = project.DefaultMargin;
                pj.StartDate = project.StartDate;
                pj.EndDate = project.EndDate;
                pj.LastUpdatedDateTime = DateTime.Now;
                pj.LastUpdatedBy = userInfo.UserID;
                pj.RowVersion += 1;

                db.SubmitChanges();

                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
            }
            catch
            {
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(ref Project pj, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                #region Kiểm tra tồn tại
                var PJ_Code = pj.ProjectCode;
                var PJ = db.Projects.Where(p => p.ProjectCode.Equals(PJ_Code)).SingleOrDefault();
                if (PJ != null)
                {
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.DUPPLICATE;
                } 
                #endregion

                Project newPJ = new Project();
                newPJ.ProgramID = pj.ProgramID;
                newPJ.ProjectCode = pj.ProjectCode;
                newPJ.ProjectName = pj.ProjectName;
                newPJ.DefaultMargin = pj.DefaultMargin;
                newPJ.StartDate = pj.StartDate;
                newPJ.EndDate = pj.EndDate;
                newPJ.CreatedBy = userinfo.UserID;
                newPJ.CreatedDateTime = DateTime.Now;
                newPJ.RowVersion = 1;
                newPJ.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                db.Projects.InsertOnSubmit(newPJ);

                db.SubmitChanges();

                pj = newPJ;

                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
            }
            catch
            {
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }
    }
}
